#include "photowidget.h"

PhotoWidget::PhotoWidget()
{
    this->init_window();
    this->init_control();
    this->init_connect();
}

PhotoWidget::~PhotoWidget()
{

}

void PhotoWidget::init_window()
{
    this->setWindowTitle("现场图片查看界面");
    this->resize(700,700);
}

void PhotoWidget::init_control()
{
    this->title = new QLabel("现场图片查看界面界面", this);
    this->title->move(180,50);
    this->title->setStyleSheet("font-size:36px");
    this->inputDate = new QLabel("请输入日期", this);
    this->inputDate->move(200,125);
    this->inputDate->setStyleSheet("font-size:20px");

    this->calendarBtn = new QPushButton("选择日期", this);
    this->calendarBtn->move(350,115);
    this->calendarBtn->setStyleSheet("width:200px");

    this->moreBtn = new QPushButton("加载更多", this);
    this->moreBtn->move(300,625);


    this->listwidget = new QListWidget(this);
    this->listwidget->move(120,175);
    this->listwidget->resize(475,420);

    this->listwidget->setViewMode(QListWidget::IconMode);//显示模式
    this->listwidget->setIconSize(QSize(100, 100));//设置图片大小
    this->listwidget->setSpacing(10);//间距
    this->listwidget->setResizeMode(QListView::Adjust);//适应布局调整
    this->listwidget->setMovement(QListView::Static);//不能移动

    this->backBtn = new QPushButton("返回", this);
    this->backBtn->move(50, 100);


    //获取该用户的照片
    char **qres=nullptr;
    int col = 0,row=0;
    int res = ImageModel::getInstance()->selectImage(qres, row, col);
    if(res==0){
        for (int i=1;i<=row;i++) {
            qDebug() << i;

            this->listwidgetItem = new QListWidgetItem(QIcon(qres[i*col]), qres[i*col+1]);
            this->listwidgetItem->setData(Qt::UserRole, QString(qres[i*col]));
            qDebug() <<  this->listwidgetItem->data(Qt::UserRole).toString();
            this->listwidget->addItem(this->listwidgetItem);
        }
        this->listwidget->setViewMode(QListView::IconMode);
        this->listwidget->setIconSize(QSize(200,200));
        this->listwidget->setResizeMode(QListWidget::Adjust);
        this->listwidget->setMovement(QListWidget::Static);
    }

    this->ImageLabel = new QLabel(this);
    //设置QLabel属性，使其支持任意大小的图片
    this->ImageLabel->setScaledContents(true);
    this->ImageLabel->setGeometry(250, 100, 400, 400);
//    this->ImageLabel->setPixmap(this->listwidgetItem->data(Qt::UserRole).toString());
    this->ImageLabel->hide();
    this->setMouseTracking(true);

    this->calendar = new QCalendarWidget(this);
    this->calendar->move(250,150);
    this->calendar->hide();


}

void PhotoWidget::init_connect()
{
    connect(this->backBtn, SIGNAL(clicked()), this, SIGNAL(backToMain()));
    connect(this->calendarBtn, SIGNAL(clicked()), this, SLOT(showCalendar()));
    connect(this->listwidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(showImg(QListWidgetItem * )));
    connect(this->calendar, SIGNAL(selectionChanged()), this, SLOT(on_calendarWidget_selectionChanged()));
}

void PhotoWidget::showImg(QListWidgetItem *image)
{
    if(this->ImageLabel->isHidden())
    {
        qDebug() << image->text();
            QString video_cover = image->data(Qt::UserRole).toString();
            qDebug() << video_cover;
            qDebug() << image->data(Qt::UserRole+1).toString();

            this->ImageLabel->setPixmap(QPixmap(video_cover));
    //        this->bannerIndex = image->data(Qt::UserRole+2).toInt();
            this->ImageLabel->show();
    }
    else {
        this->ImageLabel->hide();
    }

}

void PhotoWidget::showCalendar()
{
    this->calendar->show();
}

void PhotoWidget::on_calendarWidget_selectionChanged()
{
    QString ss = this->calendar->selectedDate().toString("yyyy-MM-dd");
    qDebug() << ss;
    this->calendarBtn->setText(this->calendar->selectedDate().toString());
    this->calendar->hide();

    this->listwidget->clear();
    //查询日期

//    //获取该用户的照片
    char **qres=nullptr;
    int col = 0,row=0;
    int res = ImageModel::getInstance()->selectImage(qres, row, col);
    if(res==0){
        for (int i=1;i<=row;i++) {
            //如果是这个日期
            qDebug() << qres[i*col+1];
            if(QString(qres[i*col+1]).contains(ss))
            {
                qDebug() << "aa";
                this->listwidgetItem = new QListWidgetItem(QIcon(qres[i*col]), qres[i*col+1]);
                this->listwidgetItem->setData(Qt::UserRole, QString(qres[i*col]));
                qDebug() <<  this->listwidgetItem->data(Qt::UserRole).toString();
                this->listwidget->addItem(this->listwidgetItem);
            }

        }
        this->listwidget->setViewMode(QListView::IconMode);
        this->listwidget->setIconSize(QSize(200,200));
        this->listwidget->setResizeMode(QListWidget::Adjust);
        this->listwidget->setMovement(QListWidget::Static);
    }
}

void PhotoWidget::closeEvent(QCloseEvent *event)
{
    QMessageBox::StandardButton resBtn = QMessageBox::question(this, "Confirm Exit",
                tr("Are you sure you want to exit?\n"),
                QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                QMessageBox::Yes);

    if (resBtn != QMessageBox::Yes) {

        event->ignore();
    } else {
//        UserModel::getInstance()->updateUserAll();
        event->accept();
    }
}

